/**
 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import {
    evaluateNumber,
    ExampleClass,
    ClassWithEmptyConstructor
} from "module"

% (@integral_primitives + @float_primitives).each do |primitive|
//! CHECKER      jscall<%= primitive.getName.capitalize %> JIT
//! RUN          force_jit: true, entry: "jscall<%= primitive.getName.capitalize %>", options: "--compiler-regex=ETSGLOBAL::jscall<%= primitive.getName.capitalize %>"
//! METHOD       "jscall<%= primitive.getName.capitalize %>"
//! PASS_AFTER   "IrBuilder"
//! INST_NOT     /CallStatic.*jscall::invoke/
//! INST         "Intrinsic.JSRuntimeGetValueDouble"

//! CHECKER      jsnewGetProperty<%= primitive.getName.capitalize %> JIT
//! RUN          force_jit: true, entry: "jsnewGetProperty<%= primitive.getName.capitalize %>", options: "--compiler-regex=ETSGLOBAL::jsnewGetProperty<%= primitive.getName.capitalize %>"
//! METHOD       "jsnewGetProperty<%= primitive.getName.capitalize %>"
//! PASS_AFTER   "IrBuilder"
//! INST_NOT     /CallStatic.*jsnew::invoke/
//! INST_COUNT   "Intrinsic.JSRuntimeGetPropertyDouble", 2
//! INST_NOT     "Intrinsic.JSRuntimeGetPropertyJSValue"
//! INST_NOT     "Intrinsic.JSRuntimeGetValueDouble"

//! CHECKER      jsnewSetProperty<%= primitive.getName.capitalize %> JIT
//! RUN          force_jit: true, entry: "jsnewSetProperty<%= primitive.getName.capitalize %>", options: "--compiler-regex=ETSGLOBAL::jsnewSetProperty<%= primitive.getName.capitalize %>"
//! METHOD       "jsnewSetProperty<%= primitive.getName.capitalize %>"
//! PASS_AFTER   "IrBuilder"
//! INST_NOT     /CallStatic.*jsnew::invoke/
//! INST_COUNT   "Intrinsic.JSRuntimeSetPropertyDouble", 2
//! INST_COUNT   "Intrinsic.JSRuntimeGetPropertyDouble", 2
//! INST_NOT     "Intrinsic.JSRuntimeGetPropertyJSValue"

//! CHECKER      jscallStaticMethod<%= primitive.getName.capitalize %> JIT
//! RUN          force_jit: true, entry: "jscallStaticMethod<%= primitive.getName.capitalize() %>", options: "--compiler-regex=ETSGLOBAL::jscallStaticMethod<%= primitive.getName.capitalize %>"
//! METHOD       "jscallStaticMethod<%= primitive.getName.capitalize() %>"
//! PASS_AFTER   "IrBuilder"
//! INST_NOT     /CallStatic.*jsnew::invoke/
//! INST_NOT     /CallStatic.*jscall::invoke/
//! INST         "Intrinsic.JSRuntimeGetValueDouble"

% end

% @integral_primitives.each do |primitive|
function jscall<%= primitive.getName.capitalize() %>(): int {
    let v0: <%= primitive.getName %> = <%= primitive.getMin() %>;
    let v1: <%= primitive.getName %> = <%= primitive.getMax() %>;
    let res = evaluateNumber(v0, v1) as double;
    if (res == <%= primitive.getSum() %>) {
        return 0;
    } else {
        return 1;
    }
}

function jsnewGetProperty<%= primitive.getName.capitalize() %>(): int {
    let v0: <%= primitive.getName %> = <%= primitive.getMin() %>;
    let v1: <%= primitive.getName %> = <%= primitive.getMax() %>;
    let res_obj = new ExampleClass(v0, v1);
    let res_get_v0 = res_obj.v0 as double;
    let res_get_v1 = res_obj.v1 as double;

    if (res_get_v0 == v0 && res_get_v1 == v1) {
        return 0;
    } else {
        return 1;
    }
}

function jsnewSetProperty<%= primitive.getName.capitalize() %>(): int {
    let v0: <%= primitive.getName %> = <%= primitive.getMin() %>;
    let v1: <%= primitive.getName %> = <%= primitive.getMax() %>;
    let res_obj = new ClassWithEmptyConstructor();
    res_obj.v0 = v0;
    res_obj.v1 = v1;

    let check_js = res_obj.verifyProperties(v0, v1) as int;
    console.println(check_js);
    if (check_js == 1) {
        return 1;
    }

    let res_get_v0 = res_obj.v0 as double;
    let res_get_v1 = res_obj.v1 as double;

    if (res_get_v0 == v0 && res_get_v1 == v1) {
        return 0;
    } else {
        return 1;
    }
}

function jscallStaticMethod<%= primitive.getName.capitalize() %>(): int {
    let v0: <%= primitive.getName %> = <%= primitive.getMin() %>;
    let v1: <%= primitive.getName %> = <%= primitive.getMax() %>;
    let res = ExampleClass.evaluateNumber(v0, v1) as double;
    if (res == <%= primitive.getSum() %>) {
        return 0;
    } else {
        return 1;
    }
}

% end

% @float_primitives.each do |primitive|
function jscall<%= primitive.getName.capitalize() %>(): int {
    let v0: <%= primitive.getName %> = <%= primitive.getMin() %>;
    let v1: <%= primitive.getName %> = <%= primitive.getMax() %>;
    let res = ExampleClass.evaluateNumber(v0, v1) as double;
    if (abs(res - <%= primitive.getSum() %>) < 0.000001) {
        return 0;
    } else {
        return 1;
    }
}

function jsnewGetProperty<%= primitive.getName.capitalize() %>(): int {
    let v0: <%= primitive.getName %> = <%= primitive.getMin() %>;
    let v1: <%= primitive.getName %> = <%= primitive.getMax() %>;
    let res_obj = new ExampleClass(v0, v1);
    let res_get_v0 = res_obj.v0 as double;
    let res_get_v1 = res_obj.v1 as double;

    if (abs(res_get_v0 - v0) < 0.000001 && abs(res_get_v1 - v1) < 0.000001) {
        return 0;
    } else {
        return 1;
    }
}

function jsnewSetProperty<%= primitive.getName.capitalize() %>(): int {
    let v0: <%= primitive.getName %> = <%= primitive.getMin() %>;
    let v1: <%= primitive.getName %> = <%= primitive.getMax() %>;
    let res_obj = new ClassWithEmptyConstructor();
    res_obj.v0 = v0;
    res_obj.v1 = v1;
    let res_get_v0 = res_obj.v0 as double;
    let res_get_v1 = res_obj.v1 as double;

    if (res_get_v0 == v0 && res_get_v1 == v1) {
        return 0;
    } else {
        return 1;
    }
}

function jscallStaticMethod<%= primitive.getName.capitalize() %>(): int {
    let v0: <%= primitive.getName %> = <%= primitive.getMin() %>;
    let v1: <%= primitive.getName %> = <%= primitive.getMax() %>;
    let res = ExampleClass.evaluateNumber(v0, v1) as double;
    if (abs(res - <%= primitive.getSum() %>) < 0.000001) {
        return 0;
    } else {
        return 1;
    }
}

% end
